package com.zhengdongzulin.zuke.app.login

import android.content.Intent
import androidx.lifecycle.Observer
import com.lxj.androidktx.core.getVM
import com.lxj.androidktx.core.putString
import com.lxj.androidktx.core.sp
import com.lxj.androidktx.core.toast
import com.lxj.androidktx.livedata.StateLiveData
import com.zhengdongzulin.zuke.R
import com.zhengdongzulin.zuke.app.Api
import com.zhengdongzulin.zuke.base.*
import com.zhengdongzulin.zuke.databinding.ZkActivityForgetBinding
import com.zhengdongzulin.zuke.ext.checkPhone
import com.zhengdongzulin.zuke.ext.contrast
import com.zhengdongzulin.zuke.ext.getTextZ
import com.zhengdongzulin.zuke.ext.start

/**
 * jeff
 * 忘记密码，修改密码
 * 2021-06-22 22:34:47
 */
class ZKForgetActivity : BaseActivity<ForgetVm, ZkActivityForgetBinding>() {
    override fun getLayoutResId(): Int = R.layout.zk_activity_forget

    override fun initVM(): ForgetVm = getVM(ForgetVm::class.java)

    override fun initView() {

    }

    override fun initData() {
        var type = intent.getStringExtra("type")
        when (type) {
            "forget" -> {
                setTitleContent("忘记密码")
            }
            else -> {
                setTitleContent("修改密码")
            }
        }

        mView.getCode.setOnClickListener {
            if (mView.phone.checkPhone()) {
                mViewModel.send_code(
                    mView.phone.getTextZ(),
                    if (intent.getStringExtra("type") == "forget") "2" else "3"
                )
            } else {
                mView.getCode.cancelCountDown()
                toast("请输入正确的手机号")
            }
        }
        mView.commit.setOnClickListener {
            if (!mView.phone.checkPhone()) {
                toast("请输入正确的手机号")
                return@setOnClickListener
            }
            if (!mView.pwd.contrast(4)) {
                toast("验证码不能少于4位")
                return@setOnClickListener
            }
            if (!mView.pwd1.contrast(6)) {
                toast("新密码不能少于6位")
                return@setOnClickListener
            }
            if (mView.pwd1.getTextZ() != mView.pwd2.getTextZ()) {
                toast("两次密码输入不一致")
                return@setOnClickListener
            }
            mViewModel.forgot_password(
                mView.phone.getTextZ(), mView.pwd.getTextZ(), mView.pwd1.getTextZ(),
                if (intent.getStringExtra("type") == "forget") Api.FORGOT_PASSWORD else Api.CHANGE_PASSWORD
            )
        }
    }

    override fun initListener() {

    }

    override fun startObserve() {
        mViewModel.send_code.observe(this, Observer {
            toast("验证码发送成功")
        })
        mViewModel.send_code.state.observe(this, Observer {
            when (it) {
                StateLiveData.State.Error, StateLiveData.State.Empty -> {
                    toast(mViewModel.send_code.errMsg)
                    mView.getCode.cancelCountDown()
                }
            }
        })
        mViewModel.forgot_password.observe(this, Observer {
            toast("修改密码成功")
            if (intent.getStringExtra("type") != "forget") {
                sp().putString("token", "")
                sp().putString("userInfo", "")
                mShareViewModel.userInfo.value = null
                mShareViewModel.token.value = null
                mShareViewModel.isSettingFinish.value = true
                Intent().start(this@ZKForgetActivity, LoginActivity::class.java)
            }
            finish()
        })
        mViewModel.forgot_password.state.observe(this, getObserver(mViewModel.forgot_password))
    }
}